package com.jn.glide.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.jn.glide.R;
import com.jn.glide.base.BaseActivity;
import com.jn.glide.contract.EatQQContract;
import com.jn.glide.entity.EatQQEntity;
import com.jn.glide.presenter.EatQQPresenter;
import com.jn.glide.transformation.GrayscaleTransformation;
import com.jn.glide.transformation.WatermarkTransformation;

public class EatQQActivity extends BaseActivity
        implements EatQQContract.IEatQQView, View.OnClickListener {
    private EditText mEdit;
    private Button mButton;
    private ImageView mImage;
    private ImageView mImage2;
    private EatQQContract.IEatQQPresenter mPresenter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_eatqq;
    }

    @Override
    public void init() {
        mEdit = findViewById(R.id.edit);
        mButton = findViewById(R.id.button);
        mImage = findViewById(R.id.image);
        mImage2 = findViewById(R.id.image2);
        mButton.setOnClickListener(this);
        mPresenter = new EatQQPresenter(this);
    }

    @Override
    public void showData(EatQQEntity entity) {
        if (entity != null) {
            String url = entity.getData();
            Glide.with(this)
                    //.asGif()
                    //.asBitmap()//写在load之前
                    .load(url)
                    //.placeholder(R.mipmap.loding) // 占位图
                    //.error(R.mipmap.error) //错误时显示的图
                    //.fallback(R.drawable.ic_launcher_background)//请求url为null时的占位图
                    //.circleCrop() // 圆形裁剪
                    //.centerCrop() // 居中裁剪
                    //.apply(RequestOptions.bitmapTransform(new WatermarkTransformation("Gif Glide Demo")))//加水印
                    //.override(500, 500)
                    //.transition(DrawableTransitionOptions.withCrossFade())//定义 Glide 如何从占位符到新加载的图片 简化写法
                    //.transform(new CircleCrop()) // 圆形
                    //.transform(new RoundedCorners(100)) //圆角
                    //.transform(new GranularRoundedCorners(20, 30, 40, 50))//四个角分别设置圆角
                    //.transform(new Rotate(-90)) //旋转
                    //.transform(new GrayscaleTransformation())//自定义
                    //.skipMemoryCache(true)//不使用内存缓存
                    //.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .into(mImage);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String qq = mEdit.getText().toString();
            if (!TextUtils.isEmpty(qq)) {
                mPresenter.getData(qq);
            }
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {
        if (loading) {
            showLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }
}
